Skip to content

Fix GH-18120: Honor FILE_SKIP_EMPTY_LINES even when FILE_IGNORE_NEW_LINES is not set#19346

Open
alexandre-daubois wants to merge 3 commits intophp:masterfrom
alexandre-daubois:empty-lines-fix
Open

Fix GH-18120: Honor FILE_SKIP_EMPTY_LINES even when FILE_IGNORE_NEW_LINES is not set#19346
alexandre-daubois wants to merge 3 commits intophp:masterfrom
alexandre-daubois:empty-lines-fix

Conversation

@alexandre-daubois
Copy link
Copy Markdown
Member

@alexandre-daubois alexandre-daubois commented Aug 1, 2025

Fix #18120

Allows to use FILE_SKIP_EMPTY_LINES without FILE_IGNORE_NEW_LINES:

$test_file = __DIR__ . "/file_skip_empty_lines.tmp";

$test_data = "First\n\nSecond\n\n\nThird\n";
file_put_contents($test_file, $test_data);

$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

/**
array(3) {
  [0]=>
  string(6) "First
"
  [1]=>
  string(7) "Second
"
  [2]=>
  string(6) "Third
"
}
*/

@alexandre-daubois
Copy link
Copy Markdown
Member Author

Friendly ping @DanielEScherzer @nielsdos as you participated on the issue 🙂

@ndossche
Copy link
Copy Markdown
Member

I'll play with this tomorrow or so. Note that this is fixing long standing behaviour, so targeting 8.5/master is likely more appropriate.

@DanielEScherzer
Copy link
Copy Markdown
Member

^ what @nielsdos said about not targeting 8.3, but wearing my release manager hat I don't think 8.5 is appropriate

@ndossche
Copy link
Copy Markdown
Member

<?php
$test_file = __DIR__ . "/file_skip_empty_lines.tmp";

$test_data = "\r";
file_put_contents($test_file, $test_data);
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

$test_data = "\r\r";
file_put_contents($test_file, $test_data);
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

produces:

array(1) {
  [0]=>
" string(1) "
}
array(0) {
}

which seems incorrect to me. Can you explain whether this is the desired behaviour?

@alexandre-daubois alexandre-daubois force-pushed the empty-lines-fix branch 2 times, most recently from 25718ef to 43d24d1 Compare December 2, 2025 14:07
@alexandre-daubois alexandre-daubois changed the base branch from PHP-8.3 to master December 2, 2025 14:07
@alexandre-daubois alexandre-daubois force-pushed the empty-lines-fix branch 2 times, most recently from d8ff4fd to daf2b51 Compare December 2, 2025 14:13
@alexandre-daubois
Copy link
Copy Markdown
Member Author

@ndossche I addressed your concern and updated the test file as well. Also, I rebased on master as suggested.

@ndossche
Copy link
Copy Markdown
Member

This still acts in a way I don't expect:

$test_data = "\r\r\n\n";
file_put_contents($test_file, $test_data);
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

There are only empty lines in this test file, so the output should be an empty array. Am I misunderstanding something? Admittingly it's been a while since I looked at this.

@alexandre-daubois
Copy link
Copy Markdown
Member Author

alexandre-daubois commented Mar 26, 2026

I took over the PR again, it fixes cases mentioned in the review while being lighter.

@ndossche I added your latest test case to ext/standard/tests/file/file_skip_empty_lines.phpt as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FILE_SKIP_EMPTY_LINES only works with FILE_IGNORE_NEW_LINES

3 participants